package com.wei.czz.framework.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wei.czz.common.tool.PageDto;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wei.czz.framework.admin.dao.DeptUserFlowDao;
import com.wei.czz.framework.admin.entity.DeptUserFlowEntity;
import com.wei.czz.framework.admin.service.DeptUserFlowService;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2022-09-21 23:10:02
 * className: DeptUserFlowServiceImpl 用户切换部门操作服务接口实现类
 * version: 1.0
 * description:
 */
@Service("userDeptFlowService")
@AllArgsConstructor
public class DeptUserFlowServiceImpl extends ServiceImpl<DeptUserFlowDao, DeptUserFlowEntity> implements DeptUserFlowService {

    private static final Logger log = LoggerFactory.getLogger(DeptUserFlowServiceImpl.class);

    @Override
    public void saveOrEdit(DeptUserFlowEntity userDeptFlow) {
        Long id = userDeptFlow.getId();
        if (Objects.isNull(id)) {
            int count = baseMapper.insert(userDeptFlow);
            log.info("插入用户关联部门流水完成。count={}", count);
        } else {

            int count = baseMapper.updateById(userDeptFlow);
            log.info("修改用户关联部门流水完成。count={}", count);
        }
    }

    @Transactional
    @Override
    public void batchSave(List<DeptUserFlowEntity> userDeptFlowList) {
        boolean bool = this.saveBatch(userDeptFlowList);
        log.info("保存用户切换部门流水完成。size={} bool={}", userDeptFlowList.size(), bool);
    }

    @Override
    public PageDto<DeptUserFlowEntity> getPageList(Long userId, Integer page, Integer limit) {

        Page<DeptUserFlowEntity> mpPage = new Page<>(page, limit);

        LambdaQueryWrapper<DeptUserFlowEntity> userDeptFlowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 匹配用户主键
        userDeptFlowLambdaQueryWrapper.eq(DeptUserFlowEntity::getUserId, userId);
        // 设置排序方式
        userDeptFlowLambdaQueryWrapper.orderByDesc(DeptUserFlowEntity::getId);
        // 查询用户切换部门流水表，获取用户切换部门流水数据
        baseMapper.selectPage(mpPage, userDeptFlowLambdaQueryWrapper);
        // 封装返回
        return new PageDto<>(mpPage);
    }

    @Override
    public List<DeptUserFlowEntity> getList(Set<Long> userIdSet) {
        LambdaQueryWrapper<DeptUserFlowEntity> userDeptFlowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 匹配 用户主键 字段
        userDeptFlowLambdaQueryWrapper.in(DeptUserFlowEntity::getUserId, userIdSet);
        // 查询数据库用户切换部门流水表，获取用户切换部门记录数据
        return baseMapper.selectList(userDeptFlowLambdaQueryWrapper);
    }

}